home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 6_10.lha / 6_10 / 6_10add.c < prev    next >
Text File  |  1993-08-08  |  748b  |  53 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. *
  6.    Add u[1..n] + v[1..n] to form w[0..n]
  7.  
  8.    Based on:
  9.  
  10.    The Art of Computer Programming, volume 2
  11.    D. Knuth, Section 4.3.1, Algorithm A
  12.  
  13.    modified to ignore w[0]
  14. /
  15. include <lint.h>
  16.  
  17. INT operator+(LINT u, LINT v)
  18.  
  19.    LINT w;
  20.  
  21.    /*
  22. A1 [Initialize]
  23.     set j <- n
  24.     k <- 0
  25.    */
  26.    for (int j = 3, k = 0; ; )
  27. {
  28. /*
  29.     A2(a) [Add digits]
  30.     set w[j] <- (u[j] + v[j] + k) mod b
  31. */
  32. LINT_Ltype t = u.s[j];
  33. t += v.s[j];
  34. t += k;
  35. w.s[j] = t;    //  % LINT_base
  36.  
  37. /*
  38.     A3 [Loop on j]
  39.     decrease j by one
  40. */
  41. if (--j < 0)
  42.     break;
  43.  
  44. /*
  45.     A2(b)
  46.     k <- (u[j] + v[j] + k) / b
  47. */
  48. k = t / LINT_base;
  49. }
  50.  
  51.    return w;
  52.  
  53.